<template>
	<view class="center">
		<view class="logo login-img" :style="{'background-image':`url(${dynamicBackground})`}">
			<block v-if="login">
				<image class="logo-img" :src="userInfo.avatar ? userInfo.avatar : avatarUrl" @click="getUserInfo"></image>
				<view class="logo-title">
					<text class="uer-name">Hi，{{login ? userInfo.realname : '您未登录'}}</text>
				</view>
			</block>
			<block v-else>
				<button @click="getUserInfo">获取用户头像昵称</button>
				<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">登录酷延保</button>
			</block>
		</view>
		
		<uni-section class="pub-title" title="订单统计" type="line"></uni-section>
		<uni-card class="card-pub wid100" :title="carInfo.name" :isShadow="true">
			<view class="pre25" style="width:25%">
				<p class="wid100">待付款</p>
				<p class="wid100 font-red">{{dingdzt[0]}}</p>
			</view>
			<view class="pre25" style="width:25%">
				<p class="wid100">已付款</p>
				<p class="wid100 font-red">{{dingdzt[1]}}</p>
			</view>
			<view class="pre25" style="width:25%">
				<p class="wid100">退款中</p>
				<p class="wid100 font-red">{{dingdzt[2]}}</p>
			</view>
			<view class="pre25" style="width:25%">
				<p class="wid100">已完成</p>
				<p class="wid100 font-red">{{dingdzt[3]}}</p>
			</view>
		</uni-card>
		
		<uni-section class="pub-title" title="功能列表" type="line"></uni-section>
		<view class="center-list">
			<view class="center-list-item border-bottom" @click="navigateTo('../../pages2/myOrderList/myOrderList', true)">
				<i class="iconfont list-icon">&#x77;</i>
				<text class="list-text">我的订单</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
			<!-- <view class="center-list-item border-bottom" @click="navigateTo('../../pages2/myTeam/myTeam', true)">
				<i class="iconfont list-icon">&#xe62c;</i>
				<text class="list-text">我的团队</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view> -->
			<view class="center-list-item border-bottom" @click="navigateTo('../../pages2/myQr/myQr', true)">
				<i class="iconfont list-icon">&#xe951;</i>
				<text class="list-text">我的二维码</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
			<view class="center-list-item border-bottom" @click="navigateTo('../../pages2/myHexm/myHexm', true)">
				<i class="iconfont list-icon">&#xe951;</i>
				<text class="list-text">我的核销码</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
			<view class="center-list-item border-bottom" @click="navigateTo('../../pages2/selfservice/selfservice', true)">
				<i class="iconfont list-icon">&#xe87a;</i>
				<text class="list-text">自助报修</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
			<view class="center-list-item border-bottom" @click="navigateTo('../../pages2/saoys/saoys', true)" v-if="showSaoys">
				<i class="iconfont list-icon">&#xe627;</i>
				<text class="list-text">扫一扫</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
			<view class="center-list-item border-bottom" @click="navigateTo('../../pages2/yanc/yanc', true)" v-if="showYanc">
				<i class="iconfont list-icon">&#xe627;</i>
				<text class="list-text">核保验车</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
			<view class="center-list-item border-bottom" @click="navigateTo('../../pages2/about/about', false)">
				<i class="iconfont list-icon">&#xe645;</i>
				<text class="list-text">关于酷车俱乐部</text>
				<text class="navigat-arrow">&#xe65e;</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dynamicBackground: "https://yjb.zjic.com.cn/minio/zhongjbx/temp/微信图片_20211110233509_1636558558025.jpg",
				dingdzt: null,
				isReady: false,
				showYanc: false,
				showSaoys: false,
				temp: {},
				login: false,
				avatarUrl: "../../static/user.png",
				userInfo: {}
			}
		},

		onLoad() {
			let that = this
			
			uni.request({
				url: getApp().globalData.serviceUrl + "/sys/dictItem/list",
				data: {
					dictId: "1458605141797699585",
				},
				dataType: 'json',
				method: 'GET',
				success: (result) => {
					if (result.data.success) {
						that.dynamicBackground = result.data.result.records[0].itemValue
					}
				}
			});
			
			if (uni.getStorageSync("userInfo") && uni.getStorageSync("token")) {
				this.userInfo = uni.getStorageSync("userInfo")
				this.login = true
				
				let roleList = uni.getStorageSync("userInfo").roleList
				if(roleList) {
					roleList.forEach(element => {
						if(element == "yewy")
							this.showYanc = true
						
						if(element == "fuws")
							this.showSaoys = true
							
					})
				}
			}
			
		},
		
		onShow() {
			let that = this
			
			uni.request({
				url: getApp().globalData.serviceUrl + "/order/kuybOrder/countGroupByDingdzt",
				data: {
					openId: uni.getStorageSync("userInfo").openId
				},
				header: {
					"X-Access-Token": uni.getStorageSync("token")
				},
				dataType: 'json',
				method: 'GET',
				success: (resp) => {
					if (resp.data.success)
						that.dingdzt = resp.data.result
					else {
						// uni.removeStorage({
						// 	key: 'openId',
						// 	success: function(res) {
						// 		console.log('success');
						// 	}
						// });
						// uni.removeStorage({
						// 	key: 'userInfo',
						// 	success: function(res) {
						// 		console.log('success');
						// 	}
						// });
						// uni.removeStorage({
						// 	key: 'token',
						// 	success: function(res) {
						// 		console.log('success');
						// 	}
						// });
						
						// uni.showToast({
						// 	title: resp.data.message,
						// 	icon: 'none'
						// });
						
						uni.login({
							provider: 'weixin',
							success: loginRes => {
								uni.showLoading({
									title: "登录中..."
								})
								uni.request({
									url: getApp().globalData.serviceUrl + "/weixin/getOpenIdByCode",
									dataType: 'json',
									method: 'GET',
									data: {
										code: loginRes.code
									},
									success: (result) => {
										console.log(result)
										if (result.data.success) {
											uni.setStorageSync('openId', result.data.result)
						
											that.loginByOpenId()
										}
									},
									complete() {
										uni.hideLoading()
									}
								})
							}
						})
						
					}
				}
			})
		},

		methods: {
			loginByOpenId() {
				let that = this
				let openId = uni.getStorageSync("openId").openid
				
				uni.request({
					url: getApp().globalData.serviceUrl + "/sys/loginByOpenId",
					data: {
						openId: openId
					},
					dataType: 'json',
					method: 'GET',
					success: (result) => {
						if (result.data.success) {
							that.isGetPhoneNumber = false
							uni.setStorageSync("token", result.data.result.token)
							uni.setStorageSync("userInfo", result.data.result)
			
						} else {
							that.isGetPhoneNumber = true
						}
					}
				});
			},
			
			updateUserInfo(userInfo) {
				uni.request({
					url: getApp().globalData.serviceUrl + "/sys/user/updateByOpenId",
					data: userInfo,
					dataType: 'json',
					method: 'PUT',
					success: (result) => {
						if (result.data.success) {
							uni.showToast({
								title: "更新成功"
							})
						}
					}
				});
				
			},
			
			getUserInfo() {
				let that = this

				uni.getUserProfile({
					desc: '申请获取用户信息',
					lang: "zh_CN",
					success: function(userProfile) {
						uni.login({
							provider: 'weixin',
							success: loginRes => {
								uni.request({
									url: getApp().globalData.serviceUrl +
										"/weixin/getOpenIdByCode",
									dataType: 'json',
									method: 'GET',
									data: {
										code: loginRes.code
									},
									success: (result) => {
										if (result.data.success) {
											that.temp = userProfile.userInfo
											that.temp.openid = result.data.result.openid
											that.temp.session_key = result.data.result.session_key
											
											that.isReady = true
											that.updateUserInfo(userProfile.userInfo)
											
										}
									}
								})

							}
						})
					},
					fail: function(error) {}
				});
			},

			getPhoneNumber(e) {
				let that = this
				console.log(this.temp)
				
				if(!that.isReady) {
					uni.showToast({
						title: '请先点击获取用户头像昵称按钮后再点击登录酷延保!',
						icon: 'none'
					});
					
					return
				}

				uni.request({
					url: getApp().globalData.serviceUrl + "/weixin/bindMobile",
					data: {
						openId: that.temp.openid,
						sessionKey: that.temp.session_key,
						iv: e.detail.iv,
						encryptedData: e.detail.encryptedData,
						nickName: that.temp.nickName,
						avatarUrl: that.temp.avatarUrl
					},
					dataType: 'json',
					method: 'GET',
					success: (result) => {
						if (result.data.success) {
							uni.setStorageSync("token", result.data.result.token)
							uni.setStorageSync("userInfo", result.data.result)

							that.userInfo = uni.getStorageSync("userInfo")
							that.login = true
						}
					}
				});
			},

			navigateTo(url, check) {
				if (check) {
					let token = uni.getStorageSync("token")
					if (token == "") {
						uni.showToast({
							title: '未登录，点击上面的按钮授权获取用户信息后才能进入!',
							icon: 'none'
						});

						return
					}
				}

				uni.navigateTo({
					url: url
				})
			},
		}
	}
</script>

<style>
	@font-face {
		font-family: texticons;
		font-weight: normal;
		font-style: normal;
		src: url('https://at.alicdn.com/t/font_984210_5cs13ndgqsn.ttf') format('truetype');
	}

	page,
	view {
		display: flex;
	}

	page {
		background-color: #f8f8f8;
	}

	.center {
		flex-direction: column;
	}

	.logo {
		width: 750upx;
		height: 340upx;
		padding: 20upx;
		box-sizing: border-box;
		flex-direction: row;
		align-items: center;
	}

	.logo-hover {
		opacity: 0.8;
	}

	.logo-img {
		width: 150upx;
		height: 150upx;
		border-radius: 150upx;
	}

	.logo-title {
		height: 150upx;
		flex: 1;
		align-items: center;
		justify-content: space-between;
		flex-direction: row;
		margin-left: 20upx;
	}

	.uer-name {
		height: 60upx;
		line-height: 60upx;
		font-size: 38upx;
		color: #FFFFFF;
	}

	.go-login.navigat-arrow {
		font-size: 38upx;
		color: #FFFFFF;
	}

	.login-title {
		height: 150upx;
		align-items: self-start;
		justify-content: center;
		flex-direction: column;
		margin-left: 20upx;
	}

	.center-list {
		background-color: #FFFFFF;
		margin-top: 20upx;
		width: 750upx;
		flex-direction: column;
	}

	.center-list-item {
		height: 100upx;
		width: 750upx;
		box-sizing: border-box;
		flex-direction: row;
		padding: 10upx 20upx;
	}

	.border-bottom {
		border-bottom-width: 1upx;
		border-color: #eeeeee;
		border-bottom-style: solid;
	}

	.list-icon {
		width: 40upx;
		height: 90upx;
		line-height: 90upx;
		font-size: 34upx;
		color: #f83d38;
		text-align: center;
		font-family: texticons;
		margin-right: 20upx;
	}

	.list-text {
		height: 90upx;
		line-height: 90upx;
		font-size: 34upx;
		color: #555;
		flex: 1;
		text-align: left;
	}

	.navigat-arrow {
		height: 90upx;
		width: 40upx;
		line-height: 90upx;
		font-size: 25upx;
		color: #999;
		text-align: right;
		font-family: texticons;
	}
		
</style>
